﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Clannan.Helper;
using Domain;
using Service.Interface;
using Spring.Context;
using Spring.Context.Support;

namespace Clannan.WebService
{
    /// <summary>
    /// Summary description for SendInfoByUserInsert
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class SendInfoByUserInsert : System.Web.Services.WebService
    {

        //spring获取对象
        IApplicationContext context = ContextRegistry.GetContext();

        public IUserInfoService UserInfoService;
        public INoteBookService NoteBookService;
        public IMobilePageService MobilePageService;

        [WebMethod]
        public int SendRegisterInfoToClient(string user)
        {
            UserInfoService = context.GetObject("Service.UserInfo") as IUserInfoService;
            string[] userMsg = user.Split('+');
            try
            {
                string passWord = RegisterMsg();
                string msg = "您绑定注册密码是：" + passWord + "，帐号为本机号码【Clannan】";
                UserInfo userInfo = new UserInfo();
                //保存用户资料
                userInfo.Name = userMsg[1];
                userInfo.Email = "";
                userInfo.Mobile = userMsg[0];
                userInfo.Password = passWord;
                userInfo.RegistData = DateTime.Now;
                userInfo.Role = new Role { ID = 1 };
                userInfo.Activate = true;
                this.UserInfoService.Save(userInfo);
                SendFetion sendMsg = new SendFetion();
                sendMsg.Send(userMsg[0], msg);
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        [WebMethod]
        public string UserLogin(string userName,string userPwd)
        {
            UserInfoService = context.GetObject("Service.UserInfo") as IUserInfoService;
            UserInfo userinfo = this.UserInfoService.GetByMobile(userName, userPwd);
            if (userinfo == null)
            {
                return "0";
            }
            return userinfo.Name;
        }

        [WebMethod]
        public int InsertNewNotePage(string pageName, string content,string userName)
        {
            MobilePageService = context.GetObject("Service.MobilePage") as IMobilePageService;
            try
            {
                MobilePage mobilepage = new MobilePage();
                mobilepage.Name = pageName;
                mobilepage.Content = content;
                mobilepage.CreateTime = DateTime.Now;
                mobilepage.MarkDelete = false;
                mobilepage.UserInfo = new UserInfo { ID = Convert.ToInt32(User.Identity.Name) };
                this.MobilePageService.Save(mobilepage);
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// 生成随机数或者字母作为注册密码
        /// </summary>
        /// <returns></returns>
        private string RegisterMsg()
        {
            int number; //绘制8个随机数或字母
            char code;
            string checkCode = String.Empty;
            Random randomCode = new Random();
            for (int i = 0; i < 8; i++)
            {
                number = randomCode.Next();
                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));
                checkCode += code.ToString();
            }
            return checkCode;
        }
    }
}
